home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / phoonsrc / original / phase.c < prev    next >
C/C++ Source or Header  |  1988-08-27  |  14KB  |  496 lines

  1. #ifndef lint
  2. static char rcsid[] =
  3.     "@(#) $Header: phase.c,v 1.2 88/08/26 22:29:42 jef Exp $ (LBL)";
  4. #endif
  5.  
  6. /* phase.c - routines to calculate the phase of the moon
  7. **
  8. ** Adapted from "moontool.c" by John Walker, Release 2.0.
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include "tws.h"
  15.  
  16. /* Astronomical constants. */
  17.  
  18. #define epoch        2444238.5       /* 1980 January 0.0 */
  19.  
  20. /* Constants defining the Sun's apparent orbit. */
  21.  
  22. #define elonge        278.833540       /* ecliptic longitude of the Sun
  23.                         at epoch 1980.0 */
  24. #define elongp        282.596403       /* ecliptic longitude of the Sun at
  25.                         perigee */
  26. #define eccent      0.016718       /* eccentricity of Earth's orbit */
  27. #define sunsmax     1.495985e8     /* semi-major axis of Earth's orbit, km */
  28. #define sunangsiz   0.533128       /* sun's angular size, degrees, at
  29.                         semi-major axis distance */
  30.  
  31. /* Elements of the Moon's orbit, epoch 1980.0. */
  32.  
  33. #define mmlong      64.975464      /* moon's mean lonigitude at the epoch */
  34. #define mmlongp     349.383063       /* mean longitude of the perigee at the
  35.                         epoch */
  36. #define mlnode        151.950429       /* mean longitude of the node at the
  37.                         epoch */
  38. #define minc        5.145396       /* inclination of the Moon's orbit */
  39. #define mecc        0.054900       /* eccentricity of the Moon's orbit */
  40. #define mangsiz     0.5181         /* moon's angular size at distance a
  41.                         from Earth */
  42. #define msmax       384401.0       /* semi-major axis of Moon's orbit in km */
  43. #define mparallax   0.9507       /* parallax at distance a from Earth */
  44. #define synmonth    29.53058868    /* synodic month (new Moon to new Moon) */
  45. #define lunatbase   2423436.0      /* base date for E. W. Brown's numbered
  46.                         series of lunations (1923 January 16) */
  47.  
  48. /* Properties of the Earth. */
  49.  
  50. #define earthrad    6378.16       /* radius of Earth in kilometres */
  51.  
  52.  
  53. #define PI 3.14159265358979323846  /* assume not near black hole nor in
  54.                         Tennessee */
  55.  
  56. /* Handy mathematical functions. */
  57.  
  58. #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))      /* extract sign */
  59. #define abs(x) ((x) < 0 ? (-(x)) : (x))           /* absolute val */
  60. #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* fix angle      */
  61. #define torad(d) ((d) * (PI / 180.0))              /* deg->rad      */
  62. #define todeg(d) ((d) * (180.0 / PI))              /* rad->deg      */
  63. #define dsin(x) (sin(torad((x))))              /* sin from deg */
  64. #define dcos(x) (cos(torad((x))))              /* cos from deg */
  65.  
  66.  
  67. /* jdate - convert internal GMT date and time to Julian day and fraction */
  68.  
  69. static long jdate(t)
  70. struct tws *t;
  71. {
  72.     long c, m, y;
  73.  
  74.     y = t->tw_year + 1900;
  75.     m = t->tw_mon + 1;
  76.     if (m > 2)
  77.        m = m - 3;
  78.     else {
  79.        m = m + 9;
  80.        y--;
  81.     }
  82.     c = y / 100L;        /* compute century */
  83.     y -= 100L * c;
  84.     return t->tw_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  85.         (m * 153L + 2) / 5 + 1721119L;
  86. }
  87.  
  88. /* jtime - convert internal date and time to astronomical Julian
  89. **         time (i.e. Julian date plus day fraction, expressed as
  90. **         a double)
  91. */
  92.  
  93. double jtime(t)
  94. struct tws *t;
  95. {
  96.     int c;
  97.  
  98.     c = - t->tw_zone;
  99.     if ( t->tw_flags & TW_DST )
  100.         c += 60;
  101.  
  102.     return (jdate(t) - 0.5) + 
  103.        (t->tw_sec + 60 * (t->tw_min + c + 60 * t->tw_hour)) / 86400.0;
  104. }
  105.  
  106. /* jyear - convert Julian date to year, month, day, which are
  107. **         returned via integer pointers to integers
  108. */
  109.  
  110. static void jyear(td, yy, mm, dd)
  111. double td;
  112. int *yy, *mm, *dd;
  113. {
  114.     double j, d, y, m;
  115.  
  116.     td += 0.5;           /* astronomical to civil */
  117.     j = floor(td);
  118.     j = j - 1721119.0;
  119.     y = floor(((4 * j) - 1) / 146097.0);
  120.     j = (j * 4.0) - (1.0 + (146097.0 * y));
  121.     d = floor(j / 4.0);
  122.     j = floor(((4.0 * d) + 3.0) / 1461.0);
  123.     d = ((4.0 * d) + 3.0) - (1461.0 * j);
  124.     d = floor((d + 4.0) / 4.0);
  125.     m = floor(((5.0 * d) - 3) / 153.0);
  126.     d = (5.0 * d) - (3.0 + (153.0 * m));
  127.     d = floor((d + 5.0) / 5.0);
  128.     y = (100.0 * y) + j;
  129.     if (m < 10.0)
  130.        m = m + 3;
  131.     else {
  132.        m = m - 9;
  133.        y = y + 1;
  134.     }
  135.     *yy = y;
  136.     *mm = m;
  137.     *dd = d;
  138. }
  139.  
  140. /* jhms - convert Julian time to hour, minutes, and seconds */
  141.  
  142. static void jhms(j, h, m, s)
  143. double j;
  144. int *h, *m, *s;
  145. {
  146.     long ij;
  147.  
  148.     j += 0.5;           /* astronomical to civil */
  149.     ij = (j - floor(j)) * 86400.0;
  150.     *h = ij / 3600L;
  151.     *m = (ij / 60L) % 60L;
  152.     *s = ij % 60L;
  153. }
  154.  
  155. /* meanphase - calculates mean phase of the Moon for a given base date
  156. **               and desired phase:
  157. **             0.0   New Moon
  158. **             0.25  First quarter
  159. **             0.5   Full moon
  160. **             0.75  Last quarter
  161. **         Beware!!!  This routine returns meaningless
  162. **               results for any other phase arguments.  Don't
  163. **         attempt to generalise it without understanding
  164. **         that the motion of the moon is far more complicated
  165. **         that this calculation reveals.
  166. */
  167.  
  168. static double meanphase(sdate, phase, usek)
  169. double sdate, phase;
  170. double *usek;
  171. {
  172.     int yy, mm, dd;
  173.     double k, t, t2, t3, nt1;
  174.  
  175.     jyear(sdate, &yy, &mm, &dd);
  176.  
  177.     k = (yy + ((mm - 1) * (1.0 / 12.0)) - 1900) * 12.3685;
  178.  
  179.     /* Time in Julian centuries from 1900 January 0.5. */
  180.     t = (sdate - 2415020.0) / 36525;
  181.     t2 = t * t;           /* square for frequent use */
  182.     t3 = t2 * t;           /* cube for frequent use */
  183.  
  184.     *usek = k = floor(k) + phase;
  185.     nt1 = 2415020.75933 + synmonth * k
  186.           + 0.0001178 * t2
  187.           - 0.000000155 * t3
  188.           + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  189.  
  190.     return nt1;
  191. }
  192.  
  193. /* truephase - given a K value used to determine the mean phase of the
  194. **               new moon, and a phase selector (0.0, 0.25, 0.5, 0.75),
  195. **               obtain the true, corrected phase time
  196. */
  197.  
  198. static double truephase(k, phase)
  199. double k, phase;
  200. {
  201.     double t, t2, t3, pt, m, mprime, f;
  202.     int apcor = 0;
  203.  
  204.     k += phase;           /* add phase to new moon time */
  205.     t = k / 1236.85;       /* time in Julian centuries from
  206.                         1900 January 0.5 */
  207.     t2 = t * t;           /* square for frequent use */
  208.     t3 = t2 * t;           /* cube for frequent use */
  209.     pt = 2415020.75933       /* mean time of phase */
  210.          + synmonth * k
  211.          + 0.0001178 * t2
  212.          - 0.000000155 * t3
  213.          + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  214.  
  215.         m = 359.2242               /* Sun's mean anomaly */
  216.         + 29.10535608 * k
  217.         - 0.0000333 * t2
  218.         - 0.00000347 * t3;
  219.         mprime = 306.0253          /* Moon's mean anomaly */
  220.         + 385.81691806 * k
  221.         + 0.0107306 * t2
  222.         + 0.00001236 * t3;
  223.         f = 21.2964                /* Moon's argument of latitude */
  224.         + 390.67050646 * k
  225.         - 0.0016528 * t2
  226.         - 0.00000239 * t3;
  227.     if ((phase < 0.01) || (abs(phase - 0.5) < 0.01)) {
  228.  
  229.        /* Corrections for New and Full Moon. */
  230.  
  231.        pt +=     (0.1734 - 0.000393 * t) * dsin(m)
  232.             + 0.0021 * dsin(2 * m)
  233.             - 0.4068 * dsin(mprime)
  234.             + 0.0161 * dsin(2 * mprime)
  235.             - 0.0004 * dsin(3 * mprime)
  236.             + 0.0104 * dsin(2 * f)
  237.             - 0.0051 * dsin(m + mprime)
  238.             - 0.0074 * dsin(m - mprime)
  239.             + 0.0004 * dsin(2 * f + m)
  240.             - 0.0004 * dsin(2 * f - m)
  241.             - 0.0006 * dsin(2 * f + mprime)
  242.             + 0.0010 * dsin(2 * f - mprime)
  243.             + 0.0005 * dsin(m + 2 * mprime);
  244.        apcor = 1;
  245.     } else if ((abs(phase - 0.25) < 0.01 || (abs(phase - 0.75) < 0.01))) {
  246.        pt +=     (0.1721 - 0.0004 * t) * dsin(m)
  247.             + 0.0021 * dsin(2 * m)
  248.             - 0.6280 * dsin(mprime)
  249.             + 0.0089 * dsin(2 * mprime)
  250.             - 0.0004 * dsin(3 * mprime)
  251.             + 0.0079 * dsin(2 * f)
  252.             - 0.0119 * dsin(m + mprime)
  253.             - 0.0047 * dsin(m - mprime)
  254.             + 0.0003 * dsin(2 * f + m)
  255.             - 0.0004 * dsin(2 * f - m)
  256.             - 0.0006 * dsin(2 * f + mprime)
  257.             + 0.0021 * dsin(2 * f - mprime)
  258.             + 0.0003 * dsin(m + 2 * mprime)
  259.             + 0.0004 * dsin(m - 2 * mprime)
  260.             - 0.0003 * dsin(2 * m + mprime);
  261.        if (phase < 0.5)
  262.           /* First quarter correction. */
  263.           pt += 0.0028 - 0.0004 * dcos(m) + 0.0003 * dcos(mprime);
  264.        else
  265.           /* Last quarter correction. */
  266.           pt += -0.0028 + 0.0004 * dcos(m) - 0.0003 * dcos(mprime);
  267.        apcor = 1;
  268.     }
  269.     if (!apcor) {
  270.            fprintf(stderr, "truephase() called with invalid phase selector.\n");
  271.        abort();
  272.     }
  273.     return pt;
  274. }
  275.  
  276. /* phasehunt5 - find time of phases of the moon which surround the current
  277. **                date.  Five phases are found, starting and ending with the
  278. **                new moons which bound the current lunation
  279. */
  280.  
  281. void phasehunt5(sdate, phases)
  282. double sdate;
  283. double phases[5];
  284. {
  285.     double adate, k1, k2, nt1, nt2;
  286.  
  287.     adate = sdate - 45;
  288.     nt1 = meanphase(adate, 0.0, &k1);
  289.     for ( ; ; ) {
  290.        adate += synmonth;
  291.        nt2 = meanphase(adate, 0.0, &k2);
  292.        if (nt1 <= sdate && nt2 > sdate)
  293.           break;
  294.        nt1 = nt2;
  295.        k1 = k2;
  296.     }
  297.     phases[0] = truephase(k1, 0.0);
  298.     phases[1] = truephase(k1, 0.25);
  299.     phases[2] = truephase(k1, 0.5);
  300.     phases[3] = truephase(k1, 0.75);
  301.     phases[4] = truephase(k2, 0.0);
  302. }
  303.  
  304.  
  305. /* phasehunt2 - find time of phases of the moon which surround the current
  306. **                date.  Two phases are found.
  307. */
  308.  
  309. void phasehunt2(sdate, phases, which)
  310. double sdate;
  311. double phases[2];
  312. double which[2];
  313. {
  314.     double adate, k1, k2, nt1, nt2;
  315.  
  316.     adate = sdate - 45;
  317.     nt1 = meanphase(adate, 0.0, &k1);
  318.     for ( ; ; ) {
  319.        adate += synmonth;
  320.        nt2 = meanphase(adate, 0.0, &k2);
  321.        if (nt1 <= sdate && nt2 > sdate)
  322.           break;
  323.        nt1 = nt2;
  324.        k1 = k2;
  325.     }
  326.     phases[0] = truephase(k1, 0.0);
  327.     which[0] = 0.0;
  328.     phases[1] = truephase(k1, 0.25);
  329.     which[1] = 0.25;
  330.     if ( phases[1] <= sdate ) {
  331.        phases[0] = phases[1];
  332.        which[0] = which[1];
  333.        phases[1] = truephase(k1, 0.5);
  334.        which[1] = 0.5;
  335.        if ( phases[1] <= sdate ) {
  336.           phases[0] = phases[1];
  337.           which[0] = which[1];
  338.           phases[1] = truephase(k1, 0.75);
  339.           which[1] = 0.75;
  340.           if ( phases[1] <= sdate ) {
  341.          phases[0] = phases[1];
  342.          which[0] = which[1];
  343.          phases[1] = truephase(k2, 0.0);
  344.          which[1] = 0.0;
  345.           }
  346.        }
  347.     }
  348. }
  349.  
  350.  
  351. /* kepler - solve the equation of Kepler */
  352.  
  353. static double kepler(m, ecc)
  354. double m, ecc;
  355. {
  356.     double e, delta;
  357. #define EPSILON 1E-6
  358.  
  359.     e = m = torad(m);
  360.     do {
  361.        delta = e - ecc * sin(e) - m;
  362.        e -= delta / (1 - ecc * cos(e));
  363.     } while (abs(delta) > EPSILON);
  364.     return e;
  365. }
  366.  
  367. /* phase - calculate phase of moon as a fraction:
  368. **
  369. **    The argument is the time for which the phase is requested,
  370. **    expressed as a Julian date and fraction.  Returns the terminator
  371. **    phase angle as a percentage of a full circle (i.e., 0 to 1),
  372. **    and stores into pointer arguments the illuminated fraction of
  373. **      the Moon's disc, the Moon's age in days and fraction, the
  374. **    distance of the Moon from the centre of the Earth, and the
  375. **    angular diameter subtended by the Moon as seen by an observer
  376. **    at the centre of the Earth.
  377. */
  378.  
  379. double phase(pdate, pphase, mage, dist, angdia, sudist, suangdia)
  380. double pdate;
  381. double *pphase;            /* illuminated fraction */
  382. double *mage;               /* age of moon in days */
  383. double *dist;               /* distance in kilometres */
  384. double *angdia;            /* angular diameter in degrees */
  385. double *sudist;            /* distance to Sun */
  386. double *suangdia;                  /* sun's angular diameter */
  387. {
  388.  
  389.     double Day, N, M, Ec, Lambdasun, ml, MM, MN, Ev, Ae, A3, MmP,
  390.            mEc, A4, lP, V, lPP, NP, y, x, Lambdamoon, BetaM,
  391.            MoonAge, MoonPhase,
  392.            MoonDist, MoonDFrac, MoonAng, MoonPar,
  393.            F, SunDist, SunAng;
  394.  
  395.         /* Calculation of the Sun's position. */
  396.  
  397.     Day = pdate - epoch;            /* date within epoch */
  398.     N = fixangle((360 / 365.2422) * Day);    /* mean anomaly of the Sun */
  399.     M = fixangle(N + elonge - elongp);  /* convert from perigee
  400.                              co-ordinates to epoch 1980.0 */
  401.     Ec = kepler(M, eccent);            /* solve equation of Kepler */
  402.     Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
  403.     Ec = 2 * todeg(atan(Ec));        /* true anomaly */
  404.         Lambdasun = fixangle(Ec + elongp);    /* Sun's geocentric ecliptic
  405.                                  longitude */
  406.     /* Orbital distance factor. */
  407.     F = ((1 + eccent * cos(torad(Ec))) / (1 - eccent * eccent));
  408.     SunDist = sunsmax / F;            /* distance to Sun in km */
  409.         SunAng = F * sunangsiz;        /* Sun's angular size in degrees */
  410.  
  411.  
  412.         /* Calculation of the Moon's position. */
  413.  
  414.         /* Moon's mean longitude. */
  415.     ml = fixangle(13.1763966 * Day + mmlong);
  416.  
  417.         /* Moon's mean anomaly. */
  418.     MM = fixangle(ml - 0.1114041 * Day - mmlongp);
  419.  
  420.         /* Moon's ascending node mean longitude. */
  421.     MN = fixangle(mlnode - 0.0529539 * Day);
  422.  
  423.     /* Evection. */
  424.     Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
  425.  
  426.     /* Annual equation. */
  427.     Ae = 0.1858 * sin(torad(M));
  428.  
  429.     /* Correction term. */
  430.     A3 = 0.37 * sin(torad(M));
  431.  
  432.     /* Corrected anomaly. */
  433.     MmP = MM + Ev - Ae - A3;
  434.  
  435.     /* Correction for the equation of the centre. */
  436.     mEc = 6.2886 * sin(torad(MmP));
  437.  
  438.     /* Another correction term. */
  439.     A4 = 0.214 * sin(torad(2 * MmP));
  440.  
  441.     /* Corrected longitude. */
  442.     lP = ml + Ev + mEc - Ae + A4;
  443.  
  444.     /* Variation. */
  445.     V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
  446.  
  447.     /* True longitude. */
  448.     lPP = lP + V;
  449.  
  450.     /* Corrected longitude of the node. */
  451.     NP = MN - 0.16 * sin(torad(M));
  452.  
  453.     /* Y inclination coordinate. */
  454.     y = sin(torad(lPP - NP)) * cos(torad(minc));
  455.  
  456.     /* X inclination coordinate. */
  457.     x = cos(torad(lPP - NP));
  458.  
  459.     /* Ecliptic longitude. */
  460.     Lambdamoon = todeg(atan2(y, x));
  461.     Lambdamoon += NP;
  462.  
  463.     /* Ecliptic latitude. */
  464.     BetaM = todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
  465.  
  466.     /* Calculation of the phase of the Moon. */
  467.  
  468.     /* Age of the Moon in degrees. */
  469.     MoonAge = lPP - Lambdasun;
  470.  
  471.     /* Phase of the Moon. */
  472.     MoonPhase = (1 - cos(torad(MoonAge))) / 2;
  473.  
  474.     /* Calculate distance of moon from the centre of the Earth. */
  475.  
  476.     MoonDist = (msmax * (1 - mecc * mecc)) /
  477.        (1 + mecc * cos(torad(MmP + mEc)));
  478.  
  479.         /* Calculate Moon's angular diameter. */
  480.  
  481.     MoonDFrac = MoonDist / msmax;
  482.     MoonAng = mangsiz / MoonDFrac;
  483.  
  484.         /* Calculate Moon's parallax. */
  485.  
  486.     MoonPar = mparallax / MoonDFrac;
  487.  
  488.     *pphase = MoonPhase;
  489.     *mage = synmonth * (fixangle(MoonAge) / 360.0);
  490.     *dist = MoonDist;
  491.     *angdia = MoonAng;
  492.     *sudist = SunDist;
  493.     *suangdia = SunAng;
  494.     return torad(fixangle(MoonAge));
  495. }
  496.